package com.lv.service.repo

import android.content.Context
import android.os.Parcelable
import androidx.lifecycle.LiveData
import androidx.room.*
import com.lv.service.bean.PersonsItem
import com.lv.service.room.helper.InspectionSectionConverter
import kotlinx.parcelize.Parcelize

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(
    entities = [UserInfo::class],
    version = 1,
    exportSchema = false,
)
abstract class UserDatabase : RoomDatabase() {

    abstract val userDao: UserDao

    companion object {

//        private val MIGRATION_1_2 = object : Migration(1, 2) {
//            override fun migrate(database: SupportSQLiteDatabase) {
//                database.execSQL("ALTER TABLE tb_o_info_user ADD COLUMN duty TEXT")
//            }
//        }

        private const val USER_DB_NAME = "syy_o_user_info_db"
//        private const val USER_DB_NAME = "syy_o_user_db"

        @Volatile
        private var instance: UserDatabase? = null

        @Synchronized
        fun getInstance(context: Context): UserDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                UserDatabase::class.java,
                USER_DB_NAME
            )
//                .addMigrations(MIGRATION_1_2)
                .build().also { instance = it }
        }
    }
}

//1、entity的定义
@TypeConverters(InspectionSectionConverter::class)
@Entity(tableName = "tb_o_info_user")
@Parcelize
data class UserInfo(
    @PrimaryKey
    var _id: Int,//主键
    val id: String,
    val password: String,
    var logo: String? = null,
    var name: String,
    var nickname: String? = null,
    val txaccid: String,
    val role: String? = null,//role; 经理：Z
    var phone: String,
    var issaler: String,//0 总经办 1 财务 2 业务  3 发行 4 设计部 5 网络部 6 运营部 7 行政
    val txaccidpassword: String,
    var head: String,
    var headid: String,
    var headphone: String,
    var bpro: String,//字段作废
    var book: String,
    var islogin: String,
    val invitecode: String,
    val invitecodeurl: String,
    val token: String?,
    val refreshtoken: String?,
    val isrecommend: String? = null,
    val updated: Long,
    val created: Long,
    val lastlogin: Long,
    val duty: String? = null,
    val sectionList: PersonsItem? = null,
) : Parcelable
////1、entity的定义
//
//@Entity(tableName = "tb_cniao_user")
//data class CniaoUserInfo(
//    @PrimaryKey
//    val id: Int,//主键
//    val course_permission: Boolean,
//    val token: String?,
//    @Embedded
//    val user: User?
//) {
//    @Keep
//    data class User(
//        @ColumnInfo(name = "cniao_user_id")
//        val id: Int,//用户id
//        val logo_url: String?,//用户头像
//        val reg_time: String?,//用户注册时间
//        val username: String?//用户名
//    )
//}

//2、dao层的定义
@Dao
interface UserDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertUser(user: UserInfo)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateUser(user: UserInfo)

    @Delete
    fun deleteUser(user: UserInfo)

    // = , in , like
    @Query("select * from tb_o_info_user where _id=:id")
    fun queryLiveUser(id: Int = 0): LiveData<UserInfo>

    @Query("select * from tb_o_info_user where _id=:id")
    fun queryUser(id: Int = 0): UserInfo?

    @Query("select * from tb_o_info_user where _id =:id")
    suspend fun getUser(id: Int = 0): UserInfo?
}



